package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.MealListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/28 0028
 */
public class MyAccountActiviy extends BaseSecondActivity {
    @BindView(R.id.myaccount_head)
    ExpandHeadView mMyaccountHead;
    @BindView(R.id.tv_desc)
    TextView mTvDesc;
    @BindView(R.id.report_count)
    TextView mReportCount;
    public MealListBean mMealListBean;
    public int mCaTimes;
    public String mCarAccountId;   //传到我购买的套餐界面 所需的请求参数

    @Override
    protected void initView() {
        initData();

        mReportCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCaTimes!= 0){
                    Intent i = new Intent(UIUtils.getContext(),PurchasedMealActivity.class);
                    i.putExtra("carAccountId",mCarAccountId);
                    i.putExtra("CaTimes",mCaTimes);
                    startActivity(i);
                    finish();
                }
            }
        });
    }

    private void initData() {
        String businessid = SPUtils.getString("businessid", "");
        ALog.d(businessid);
        OkHttpUtils.post().url(Constants.MEALLIST_URL).addParams("sign",Constants.SIGN).addParams("carBusinessId", businessid)
                .addParams("serviceTypeId","1").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("加载数据失败,请检查网络...");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                Gson gson= new Gson();
                mMealListBean = gson.fromJson(response, MealListBean.class);
                String success = mMealListBean.getSuccess();
                String message = mMealListBean.getMessage();
                if(TextUtils.equals("00",success)){
                    List<MealListBean.CbAccountsBean> cbAccounts = mMealListBean.getCbAccounts();
                    if(cbAccounts.size()!= 0){
                        mCarAccountId = cbAccounts.get(0).getId();
                        mCaTimes = cbAccounts.get(0).getCaTimes();
                        ALog.d("caTimes==============="+ mCaTimes);
                        mReportCount.setText("报告剩余"+ mCaTimes +"条");
                    }
                }else {
                    MyToast.show(message);
                }
            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_myaccount;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMyaccountHead;
    }


}
